//
//  FXVisitorView.swift
//  微博(Swift)
//
//  Created by V on 15/12/28.
//  Copyright © 2015年 V. All rights reserved.
//

import UIKit
import SnapKit

// MARK: - 代理
// Swift 中协议必须继承基协议（NSObjectProtocol），否则不能给 delegate 设置为 weak
/**
    代理实现步骤：
    － 被代理：
        1. 设置协议
        2. 提供一个 代理属性
        3. 在合适的时候调用 代理实现的方法

    － 代理：
        1. 遵守协议
        2. 设置自己为代理
        3. 实现协议中的方法

*/
//protocol FXVisitorViewDelegate: NSObjectProtocol {
//    func registerBtnClick()
//    func loginBtnClick()
//}





class FXVisitorView: UIView {
    
//    weak var delegate: FXVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    func setupInfo(imageName: String?, text: String?){
        if imageName != nil {
            circleImgView.hidden = true
            centerImgView.image = UIImage(named: imageName!)
            textLable.text = text
        }else{
            startAnimation()
        }
    }
    
    
    
    
    private func startAnimation(){
        // 1. 初始化动画对象
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 2. 属性
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.toValue = 2 * M_PI
        anim.removedOnCompletion = false
        circleImgView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    
    
    
    
    // MARK: - 懒加载控件
    // 中心大图
    lazy var centerImgView: UIImageView = UIImageView()
    // 圆圈
    lazy var circleImgView: UIImageView = UIImageView()
    // 文字
    lazy var textLable: UILabel = UILabel()
    // 按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
//        btn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
//        btn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    // 遮罩
    lazy var maskImgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    
    
//    // MARK: - 监听方法
//    @objc private func registerBtnClick(){
//        delegate?.registerBtnClick()
//    }
//    
//    @objc private func loginBtnClick(){
//        delegate?.loginBtnClick()
//    }
    
    
    
    

}






// MARK: - setupUI
extension FXVisitorView {
    private func setupUI(){
        // 背景图
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        
        // 添加控件
        addSubview(circleImgView)
        addSubview(maskImgView)
        addSubview(centerImgView)
        addSubview(textLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // 中心大图
        // @noescape --> 代表后面定义的闭包是在当前代码位置使, 不用使用 self
        centerImgView.image = UIImage(named: "visitordiscover_feed_image_house")
        centerImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
        
        
        
        // 圆圈
        circleImgView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        circleImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(centerImgView)
        }
        
        
        // 文字
        textLable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        textLable.tintColor = UIColor.darkGrayColor()
        textLable.font = UIFont.systemFontOfSize(14)
        textLable.textAlignment = .Center
        textLable.numberOfLines = 0
        textLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleImgView.snp_bottom).offset(16)
            make.centerX.equalTo(circleImgView.snp_centerX)
            make.width.equalTo(224)
        }
        
        
        
        // 登录注册
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        registerBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLable.snp_leading)
            make.top.equalTo(textLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
       loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(textLable.snp_right)
            make.width.equalTo(100)
            make.centerY.equalTo(registerBtn.snp_centerY)
        }
        
        
        
        
        // 遮罩
        // VFL: 可视化格式语言
        // | : 边界
        // H : 约束方向
        // metrics : 用于设置格式字符串里面的 `()` 里面的数值
        // views: 用于设置格式字符串里面的 `[]` 里面的View
        
        maskImgView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskImgView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerBtn]", options: [], metrics: ["offsetY": -35], views: ["view": maskImgView, "registerBtn": registerBtn]))
        
        
    }

}
